package com.yunji.web.cabinet;


import com.yunji.cabinet.business.UpgradeTaskBusiness;
import com.yunji.cabinet.dto.UpgradeTaskDTO;
import com.yunji.cabinet.params.UpgradeTaskParam;
import com.yunji.cabinet.vo.UpgradeTaskVO;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "管理端-升级任务")
@RestController
@RequestMapping("/upgrade/task")
public class UpgradeTaskController {

    @Resource
    private UpgradeTaskBusiness business;

    @ApiOperation("新增任务")
    @PostMapping(value = "create")
    @PreAuthorize("@ss.hasPermi('upgrade:ota:add')")
    public Response<String> create(@RequestBody UpgradeTaskDTO dto){
        return ResponseHelper.success(business.create(dto));
    }

    @ApiOperation("任务列表")
    @GetMapping(value = "list")
    public Response<List<UpgradeTaskVO>> list(UpgradeTaskParam param){
        return ResponseHelper.successWithPage(business.search(param), param.getTotal());
    }

    @ApiOperation("任务详情")
    @GetMapping(value = "/getInfo/{taskCode}")
    @PreAuthorize("@ss.hasPermi('upgrade:ota:info')")
    public Response<UpgradeTaskVO> getInfo(@PathVariable("taskCode") String taskCode){
        return ResponseHelper.success(business.select(taskCode));
    }

    @ApiOperation("终止任务")
    @GetMapping(value = "close/{taskCode}")
    @PreAuthorize("@ss.hasPermi('upgrade:ota:stop')")
    public Response<String> closeTask(@PathVariable String taskCode){
        return ResponseHelper.handle(business.closeTask(taskCode),"终止失败");
    }

}
